﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTAccess.Business;
using DCTLib.Common;
using DCTAccess.Business.Needs;
using DCTSollution.DCTLib.Common;

public partial class AdminZone_NeedManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindToGrid();
            LoadServices();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ValidateSaveChangeData())
        {
            Need need = GetNeed();

            if (NeedController.Insert(need))
            {
                lblError.Text = AppMessages.INSERT_SUCCESSFULLY;
                BindToGrid();
                txtNeed.Text = "";
                cbxAction.Checked = false;
            }
            else
            {
                lblError.Text = AppMessages.CANNOT_INSERT_VI;

            }
        }
    }

    private Need GetNeed()
    {
        Need need = new Need();
        need.NeedName = txtNeed.Text;
        need.CategoryPageId = int.Parse(ddlServices.SelectedValue);
        need.IsEnable = cbxAction.Checked;

        return need;
    }

    private void LoadServices()
    {
        ControlLoader.LoadDropDownList(this.ddlServices, NeedController.GetServices(), "ServiceID", "ServiceName");
    }

    private bool ValidateSaveChangeData()
    {
        bool validated = true;
        if (ValidationHelper.IsEmpty(txtNeed))
        {
            lblError.Text = AppMessages.RequireField("Tên dịch vụ") + "<br>";
            validated = false;
        }
        return validated;
    }

    private void BindToGrid()
    {
        DataTable dtNeed = NeedController.GetAll();
        gvNeed.DataSource = dtNeed;
        gvNeed.DataBind();
    }

    protected void gvNeed_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ControlLoader.BindDatagrid(gvNeed, NeedController.GetAll(), e.NewPageIndex);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string[] NeedDel = Request.Params.GetValues("checkDel");
        Need need;
        try
        {
            if (NeedDel.Length > 0)
            {
                for (int i = 0; i < NeedDel.Length; i++)
                {
                    need = new Need();
                    need.NeedID = int.Parse(NeedDel[i]);
                    // lblError.Text += NeedDel[i].ToString();

                    if (NeedController.Delete(need))
                    {
                        lblError.Text = AppMessages.DELETED_SUCCESSFULLY;
                        BindToGrid();
                    }
                    else
                    {
                        lblError.Text = " Cannot delete";
                    }
                }
            }
        }
        catch
        {

        }
    }
}
